/***************************************************************************
 创建者: 华磊
 开始时间: 2018.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写

 ***************************************************************************/
#include "timerinstructionlogic.h"
#include "GeneralDefine.h"
#include "messagelog.h"
#include "programengine.h"


TimerInstructionLogic::TimerInstructionLogic(int robotIdIn, ProgramSet* programSetIn,
                                             RobotIo* ioControllerIn, ProgramEngine* programEngineIn,
                                             RegData *regDataIn)
  {
      programSet=programSetIn;
      robotId=robotIdIn;
      ioController=ioControllerIn;
      programEngine=programEngineIn;
      regData=regDataIn;

      startTime = {0,0};
      endTime = {0,0};

      waitStartTime = {0,0};
      waitEndTime = {0,0};
      waitTimeStartFlag = false;


  }

//在使用ｔｉｍｅｒ时间时需要调用gettimeofday(&endTime,NULL);来获取当前时间，从而获取时间间隔差
E_PROGRAM_LOOP_RESULT TimerInstructionLogic::startTimerCmd(int programNum, TimerInstruction timerInstruct)
{
//    initProgramSet();

    int timerNum = timerInstruct.front_value - 1;

    if(timerNum >= D_ROBOT_TIMER_REG_SIZE || 0 > timerNum)
    {
        //error
        return E_PROGRAM_LOOP_RESULT_ERROR;
    }
    else
    {
        float timeCount = 0.0;
        if(TIMER_PROCESS_REG == timerInstruct.timerProcessType)
        {
                timeCount = programEngine->parseRegisterData(programNum);
//                gettimeofday(&startTime,NULL);

//                programSet.countTimer[timerNum] = timeCount;
//                programSet.startTime[timerNum] = startTime;
//                programSet.endTime[timerNum] = startTime;
//                programSet.timerProcessType[timerNum] = TIMER_PROCESS_REG;

        }
        else
        {
//             setTimerReg(timerNum,timerInstruct.timerProcessType);
        }

        regData->setRegTimer(timerNum,timerInstruct.timerProcessType,timeCount);

        programEngine->calculateNextLineToRun(programNum);

        E_PROGRAM_LOOP_RESULT endFlag = programEngine->isTheLastInstruction(programNum);
        return endFlag;
    }

}


bool TimerInstructionLogic::countWaitTime(float waitTime)
{
//    float intervalTime = waitTime * MICROSECOND_TIME;

    if(!waitTimeStartFlag)
    {
        gettimeofday(&waitStartTime,NULL);
        waitTimeStartFlag = true;
        return false;
    }
    else
    {
        gettimeofday(&waitEndTime,NULL);
        float secondTime = (float)(waitEndTime.tv_sec - waitStartTime.tv_sec);
        float micro2Second = (float)(waitEndTime.tv_usec - waitStartTime.tv_usec)/MICROSECOND_TIME;

        float intervalTime = secondTime + micro2Second;

        if(intervalTime >= waitTime)
        {
            waitTimeStartFlag = false;
            return true;
        }
        else
        {
            return false;
        }
    }
}

int TimerInstructionLogic::reset()
{
    waitTimeStartFlag = false;
}

void TimerInstructionLogic::addMsg(int messageLevel, string componentName, string messageType,
                           int messageCode, int robotId,
                           int parameter1, int parameter2, int parameter3, int parameter4,QString message)
{
    Message tmpMsg;
    tmpMsg.MessageLevel = messageLevel;
    tmpMsg.componentClassName = componentName;
    tmpMsg.messageType = messageType;
    tmpMsg.robotId = robotId;
    tmpMsg.messageCode = messageCode;
   // tmpMsg.time = t1.tv_sec;


    QString infomationStr;

    switch(messageCode)
    {
    case 0:
    {
        infomationStr =  QObject::tr("机器人:")  + QString::number(robotId);
        infomationStr = infomationStr + QObject::tr("; %1,第%2行， IO号超出已有逻辑ＩＯ范围，请检查指令或ＩＯ配置，").arg(message).arg(parameter3)
                +QString::number(parameter1) + ">="
        +QString::number(parameter2);
        break;


    }



    default:
    {

    }
    }


    tmpMsg.MessageInformation = infomationStr.toStdString();

    // qDebug()<<"53444444444444444444444"<< tmpMsg.messageCode;
    MessageLog::getInstance()->addMessage(tmpMsg);
}
